package com.xsky.logic.casetype;

import java.util.List;
import java.util.UUID;

import com.thoughtworks.xstream.XStream;
import com.thoughtworks.xstream.io.xml.DomDriver;
import com.xsky.database.dao.impl.BaseEntityDaoImpl;
import com.xsky.operation.vo.TabCaseType;

public class CaseTypeLogic
{
	public String add(String xmlDoc)//增加
	{
		BaseEntityDaoImpl baseDao = new BaseEntityDaoImpl();
		XStream xs = new XStream(new DomDriver());
		xs.alias("item", TabCaseType.class);
		TabCaseType entity = (TabCaseType)xs.fromXML(xmlDoc);
		List rootList = baseDao.findByHql("from TabCaseType model where model.fatherId='"+0+"'");
		if(rootList.size()==0)
		{
			TabCaseType root = new TabCaseType();
			root.setTypeName("根节点");
			root.setFatherId("0");
			root.setId(UUID.randomUUID().toString().substring(0,31));
			baseDao.insertEntity(root);
			entity.setId(UUID.randomUUID().toString().substring(0,31));
			entity.setFatherId(root.getId());
			baseDao.insertEntity(entity);
		}
		else
		{
			if(entity.getFatherId()==null||"".equals(entity.getFatherId()))
			{
				TabCaseType tcd = (TabCaseType)rootList.get(0);
				entity.setFatherId(tcd.getId());
			}
			entity.setId(UUID.randomUUID().toString().substring(0,31));
			baseDao.insertEntity(entity);
		}
		return "新增Case类型成功";
	}
	public String delete(String id)//删除一个文件栏目
	{
		String hql = "delete TabCaseType model where model.id='"+id+"'";
		BaseEntityDaoImpl baseDao = new BaseEntityDaoImpl();
		baseDao.execute(hql);
		baseDao.execute("delete TabCaseType model where model.fatherId='"+id+"'");
		return "删除Case类型成功";
	}
	public String update(String xmlDoc)//更新一个文件栏目
	{
		BaseEntityDaoImpl baseDao = new BaseEntityDaoImpl();
		XStream xs = new XStream(new DomDriver());
		xs.alias("item", TabCaseType.class);
		TabCaseType ft = (TabCaseType)xs.fromXML(xmlDoc);
		baseDao.updateEntity(ft);
		return "更新Case类型成功";
	}
	public String genTree(String id)//获取某个节点的部门树
	{
		BaseEntityDaoImpl baseDao = new BaseEntityDaoImpl();
		TabCaseType entity = new TabCaseType();
		entity = (TabCaseType)baseDao.findByPrimaryKey(entity, "id", id);
	String childXml = "";
		if(entity.getTypeCode()==null||"null".equals(entity.getTypeCode()))
		{
			childXml = "<item id=\""+entity.getId()+"\" remarks=\""+entity.getRemarks()+"\" label=\""+entity.getTypeName()+"\" fatherId=\""+entity.getFatherId()+"\"  typeName=\""+entity.getTypeName()+"\" referenceTime=\""+entity.getReferenceTime()+"\" typeCode=\""+entity.getTypeCode()+"\">";
		}
		else
		{
			childXml = "<item id=\""+entity.getId()+"\" remarks=\""+entity.getRemarks()+"\" label=\""+entity.getTypeCode()+"|"+entity.getTypeName()+"\" fatherId=\""+entity.getFatherId()+"\"  typeName=\""+entity.getTypeName()+"\" referenceTime=\""+entity.getReferenceTime()+"\" typeCode=\""+entity.getTypeCode()+"\">";
		}
		List fileTitleList = baseDao.findByHql("from TabCaseType model where model.fatherId='"+id+"'");
		for(int i = 0; i < fileTitleList.size(); i++)
		{
			TabCaseType childEntity = (TabCaseType)fileTitleList.get(i);
			childXml+=genTree(childEntity.getId());
		}
		childXml+="</item>";
		return childXml;
	}
	public String genTreeRootId()
	{
		String id="0";
		BaseEntityDaoImpl baseDao = new BaseEntityDaoImpl();
		List list = baseDao.findByHql("from TabCaseType model where model.fatherId='0'");
		for(int i = 0; i < list.size(); i++)
		{
			TabCaseType childEntity = (TabCaseType)list.get(i);
			id = childEntity.getId();
		}
		return id;
	}
	public String isEmpty(String id)
	{
		BaseEntityDaoImpl baseDao = new BaseEntityDaoImpl();
		List lmList = baseDao.findByHql("from TabCaseType model where model.fatherId='"+id+"'");
		if(lmList.size()>0)
		{
			return "false";
		}
		return "true";
	}
}
